$(function () {
	
	var numElements = $('#indicator div span').length;
	
	var dir=$('html').attr('dir') == 'rtl' ? 'p' : 'n';
	var curPos=dir == 'n' ? 0 : 2 ;
	
	var fadeSpeed = 300;
	var autoFadeSpeed = 6000;
	var waitInteractionTime = 25000;
	
		
	//Text-MarginTop berechnen
	var textBox=$('#text-box');
	var text=$('#text-box div');
	var pictureBox=$('#picture-box');
	var textBoxHeight=parseInt(textBox.css('height'));
	var pictureBoxHeight=parseInt($('#picture-box').css('top'));
	
	text.each(function(index){
		
		$(this).css('display','block');
		var textHeight=parseInt($(this).css('height'));
		if(textHeight>textBoxHeight){
			pictureBox.css('top',(pictureBoxHeight+textHeight-textBoxHeight)+'px');
			textBoxHeight=textHeight;
			textBox.css('height',textHeight+'px');
		}
		$(this).css('display','');	
	});
			
	//Text-MarginTop berechnen
	text.each(function(index){
		
		$(this).css('display','block');
		var textHeight=parseInt($(this).css('height'));		
		if(textHeight<textBoxHeight){
			$(this).css('padding-top',((textBoxHeight-textHeight)/2)+'px');
		}
		$(this).css('display','');		
	});		
	
	//Show first Elements
	getElements(dir);

	//AutoFade
	var AutoFade=setInterval(function(){
		getElements('n');
	},autoFadeSpeed);
			
	//Eventhandler for next & prev
	$('#next').click(function(){
		getElements(dir);		
	});
	
	$('#prev').click(function(){
		getElements(dir=='n'?'p':'n');
	});			
	
	$('#indicator div span').click(function(){
		var id=$(this).attr('id');
		curPos=id.substr(1,2)-1;			
		getElements();
	});		
	
	var waitInteraction = 0;
	
	var restartSlideShow = function(e){
		clearInterval(AutoFade);
		clearInterval(waitInteraction);
		$('#next, #prev, #indicator div span').die("click");	
		enableClick = setTimeout(function(){
			$('#next, #prev, #indicator div span').live("click", restartSlideShow);
		}, 1000);
		waitInteraction = setInterval(function(){
			clearInterval(waitInteraction);
			AutoFade = setInterval(function(){
				getElements('n');
			}, autoFadeSpeed);
		}, waitInteractionTime-autoFadeSpeed);	
	};
	
	$('#next, #prev, #indicator div span').live("click", restartSlideShow);
	
	//Get next or prev element
	function getElements(dir){
						
		if(dir=='p'){
			curPos--;					
			if(curPos<1) curPos=numElements;									
		}else{
			curPos++;
			if(curPos>numElements) curPos=1;		
		}
		
		//Animate Elements
		changeText();
		changePicture();		
		changeIndicator();		
	}	
	
	function changeText(){
		//Change text
		$('#text-box div.active').removeClass('active').stop(true, true).fadeOut(fadeSpeed, function(){
			$('#text-box div:nth-child('+curPos+')').fadeIn(fadeSpeed).addClass('active');
		});	
	}
	
	function changePicture(){
		//SimpleVersion
		$('#picture img').removeClass('active');
		$('#picture img:nth-child('+curPos+')').addClass('active');
	}
		
	function changeIndicator(){
		//Change indicator
		$('#indicator span').removeClass('highlight');
		$('#indicator span:nth-child('+curPos+')').addClass('highlight');
	}
	
	//Eventhandler for Toggle-Groups
	$('.toggle-head').click(function(){
		var el=$(this);
		if(el.hasClass('active')){
			el.removeClass('active');
			el.next().removeClass('toggle-content-active');		
		}else{
			$('.toggle *').removeClass('active');
			$('.toggle *').removeClass('toggle-content-active');
			el.addClass('active');
			el.next().addClass('toggle-content-active');	
		}	
	});
	
	
});